/*
 * Principal.java
 *
 * Created on 13 de mayo de 2008, 01:53 PM
 */

package view;

import util.ImageMouseListener;
import util.ImageThumbnail;
import util.DialogkeyListener;
import util.DataReader;
import controller.ImageViewerController;
import util.ImageDb;
import model.ImageViewerModelInterface;
import java.awt.GridLayout;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import java.util.Iterator;
import javax.swing.JOptionPane;

/**
 *
 * @author  Daniel
 */
public class GridView extends javax.swing.JFrame {
    private JFileChooser chooser;
    private File directory;        //Directorio donde se toman las imagenes
    private DataReader reader;
    private ArrayList<ImageDb> imagesDb;
    private ImageMouseListener listener;
    private ArrayList<ImageDb> display;
    
    private int current;    
    private int ROWS = 5;
    private int COLS = 4;
    private int limit = ROWS*COLS;
    private ImageViewerController controller;
    private ImageViewerModelInterface model;
    private SliderDialog slider;
    
    /** Creates new form Principal */
    public GridView(ImageViewerController controller, ImageViewerModelInterface model) {
        this.controller = controller;
        this.model = model;
        
        chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setMultiSelectionEnabled(false); 
        listener = new ImageMouseListener(this, controller, model);
        
        slider = new SliderDialog(this, controller);                
        slider.addKeyListener(new DialogkeyListener(slider));        
        
        initComponents();                
        if(controller.getTuples("pictures") != 0)
        {
            LoadImages();
        }
    }        

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        progress = new javax.swing.JProgressBar();
        labelProgress = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        jMenuFile = new javax.swing.JMenu();
        jMenuOpen = new javax.swing.JMenuItem();
        jMenuExit = new javax.swing.JMenuItem();
        jMenuEdit = new javax.swing.JMenu();
        menuBar1 = new javax.swing.JMenuBar();
        jMenuFile1 = new javax.swing.JMenu();
        jMenuOpen1 = new javax.swing.JMenuItem();
        jMenuExit1 = new javax.swing.JMenuItem();
        jMenuEdit1 = new javax.swing.JMenu();
        menuBar2 = new javax.swing.JMenuBar();
        jMenuFile2 = new javax.swing.JMenu();
        jMenuOpen2 = new javax.swing.JMenuItem();
        jMenuExit2 = new javax.swing.JMenuItem();
        jMenuEdit2 = new javax.swing.JMenu();
        menuBar3 = new javax.swing.JMenuBar();
        jMenuFile3 = new javax.swing.JMenu();
        jMenuOpen3 = new javax.swing.JMenuItem();
        jMenuExit3 = new javax.swing.JMenuItem();
        jMenuEdit3 = new javax.swing.JMenu();
        panelImg = new javax.swing.JPanel();
        buttonBack = new javax.swing.JButton();
        buttonNext = new javax.swing.JButton();
        menuBar4 = new javax.swing.JMenuBar();
        jMenuFile4 = new javax.swing.JMenu();
        jMenuOpen4 = new javax.swing.JMenuItem();
        jMenuExit4 = new javax.swing.JMenuItem();
        jMenuEdit4 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Grid View");

        progress.setStringPainted(true);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(153, Short.MAX_VALUE)
                .addComponent(labelProgress, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(progress, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
            .addComponent(labelProgress, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
        );

        jMenuFile.setText("File");

        jMenuOpen.setText("Open");
        jMenuOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuOpenActionPerformed(evt);
            }
        });
        jMenuFile.add(jMenuOpen);

        jMenuExit.setText("Exit");
        jMenuExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuExitActionPerformed(evt);
            }
        });
        jMenuFile.add(jMenuExit);

        menuBar.add(jMenuFile);

        jMenuEdit.setText("Edit");
        menuBar.add(jMenuEdit);

        jMenuFile1.setText("File");

        jMenuOpen1.setText("Open");
        jMenuOpen1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuOpenActionPerformed(evt);
            }
        });
        jMenuFile1.add(jMenuOpen1);

        jMenuExit1.setText("Exit");
        jMenuExit1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuExitActionPerformed(evt);
            }
        });
        jMenuFile1.add(jMenuExit1);

        menuBar1.add(jMenuFile1);

        jMenuEdit1.setText("Edit");
        menuBar1.add(jMenuEdit1);

        jMenuFile2.setText("File");

        jMenuOpen2.setText("Open");
        jMenuOpen2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuOpenActionPerformed(evt);
            }
        });
        jMenuFile2.add(jMenuOpen2);

        jMenuExit2.setText("Exit");
        jMenuExit2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuExitActionPerformed(evt);
            }
        });
        jMenuFile2.add(jMenuExit2);

        menuBar2.add(jMenuFile2);

        jMenuEdit2.setText("Edit");
        menuBar2.add(jMenuEdit2);

        jMenuFile3.setText("File");

        jMenuOpen3.setText("Open");
        jMenuOpen3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuOpenActionPerformed(evt);
            }
        });
        jMenuFile3.add(jMenuOpen3);

        jMenuExit3.setText("Exit");
        jMenuExit3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuExitActionPerformed(evt);
            }
        });
        jMenuFile3.add(jMenuExit3);

        menuBar3.add(jMenuFile3);

        jMenuEdit3.setText("Edit");
        menuBar3.add(jMenuEdit3);

        panelImg.setBackground(new java.awt.Color(255, 255, 255));
        panelImg.setAutoscrolls(true);

        javax.swing.GroupLayout panelImgLayout = new javax.swing.GroupLayout(panelImg);
        panelImg.setLayout(panelImgLayout);
        panelImgLayout.setHorizontalGroup(
            panelImgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 569, Short.MAX_VALUE)
        );
        panelImgLayout.setVerticalGroup(
            panelImgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 409, Short.MAX_VALUE)
        );

        buttonBack.setText("<<");
        buttonBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBackActionPerformed(evt);
            }
        });

        buttonNext.setText(">>");
        buttonNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonNextActionPerformed(evt);
            }
        });

        jMenuFile4.setText("File");

        jMenuOpen4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuOpen4.setText("Open");
        jMenuOpen4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuOpenActionPerformed(evt);
            }
        });
        jMenuFile4.add(jMenuOpen4);

        jMenuExit4.setText("Exit");
        jMenuExit4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuExitActionPerformed(evt);
            }
        });
        jMenuFile4.add(jMenuExit4);

        menuBar4.add(jMenuFile4);

        jMenuEdit4.setText("Edit");

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Show favourite ");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenuEdit4.add(jMenuItem2);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Query");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenuEdit4.add(jMenuItem1);

        menuBar4.add(jMenuEdit4);

        setJMenuBar(menuBar4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(buttonBack, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonNext, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(panelImg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonBack, buttonNext});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(panelImg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(buttonBack, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
                        .addComponent(buttonNext, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jMenuOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuOpenActionPerformed
    int result = chooser.showOpenDialog(this);
    if(result == JFileChooser.CANCEL_OPTION)
        return;
    
    directory = chooser.getSelectedFile();    
    reader = new DataReader(directory);    
    controller.insertImages(reader.read());        
    LoadImages(); //Carga las imagenes guardadas en la base
}//GEN-LAST:event_jMenuOpenActionPerformed

private void LoadImages()
{
    current = 0;
    display = model.getPictures(0, limit);
    drawImages(display);
}


public void drawImages(ArrayList<ImageDb> images)
{
    labelProgress.setText("loading...");
    progress.setMaximum(controller.getTuples("pictures") - 1 );    
    panelImg.removeAll(); 
    panelImg.setLayout(new GridLayout(ROWS, COLS, 2, 2) );    
    //imagesDb = model.getPictures(); 
    
    //int limit = ROWS*COLS;
    for(int i = 0; i < limit; i++)
    {
        if(current < controller.getTuples("pictures")){
            ImageThumbnail t = new ImageThumbnail(images.get(i));
            t.addMouseListener(listener);
            t.setPos(current);
            panelImg.add(t);            
            progress.setValue(current);
            current++;
        }
        else
            panelImg.add(new JPanel());
    }
    
    panelImg.validate();        
    labelProgress.setText("");
}

public void loadNext()
{
    display = model.getPictures(current, limit);    

    System.out.println("-----------------Next");
    System.out.println("Current: " + current);
    System.out.println("display " + display.size());
    System.out.println("limit: " + limit);    
    if(display.size() <= limit && display.size() != 0)
        drawImages(display);
    
   
}

public void loadBack()
{
    int t = current - (display.size()+ 1) - limit ;
    
    System.out.println("-----------------Back");
    System.out.println("Current: " + current);
    System.out.println("display " + display.size());
    System.out.println("limit: " + limit);
    System.out.println("t: " + t);
    if(t >= 0){
        current = t;
        display = model.getPictures(t, limit);    
        drawImages(display);    
    }
}

public void LaunchQuery(ImageDb imd){
    System.out.println("Enter querying.");
    System.out.println(imd.getName());
    System.out.println(imd.getDescription());
    System.out.println(imd.getConfidentiality());
    System.out.println(imd.getSize());
    if (imd != null)
    {
        System.out.println("Executing.");
        ArrayList<ImageDb> imagesfromquery = new ArrayList<ImageDb>();
        imagesfromquery = controller.query(imd);
        System.out.println("query results " + imagesfromquery.size());
        if(imagesfromquery != null && !imagesfromquery.isEmpty()){            
            slider.setImages(imagesfromquery, 0, imagesfromquery.size());
            slider.setVisible(true);
            printArrayList(imagesfromquery.iterator());
        }
    }
    // something to do with the imges.
}

public void printArrayList(Iterator arr){
    System.out.println("Entered printing ");
    while (arr.hasNext()){
        System.out.println("Printing ");
        ImageDb temp = (ImageDb)(arr.next());
        System.out.println(temp.getPath() + "," + temp.getName() + "," + temp.getSize());
    }
}


private void jMenuExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuExitActionPerformed
    System.exit(0);
}//GEN-LAST:event_jMenuExitActionPerformed

private void buttonBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBackActionPerformed
    loadBack();
}//GEN-LAST:event_buttonBackActionPerformed

private void buttonNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonNextActionPerformed
    loadNext();
}//GEN-LAST:event_buttonNextActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
    QueryForm qf = new QueryForm(this);    
    this.setEnabled(false);
    qf.setVisible(true);
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
    ArrayList<ImageDb> img = controller.getFavorites();
    if(img.isEmpty()){
        JOptionPane.showMessageDialog(this, "No hay favoritos");
        return;
    }
    
    slider = new SliderDialog(this, controller);                
    slider.addKeyListener(new DialogkeyListener(slider));
    slider.setImages(img, 0, img.size());
    slider.setVisible(true);
}//GEN-LAST:event_jMenuItem2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBack;
    private javax.swing.JButton buttonNext;
    private javax.swing.JMenu jMenuEdit;
    private javax.swing.JMenu jMenuEdit1;
    private javax.swing.JMenu jMenuEdit2;
    private javax.swing.JMenu jMenuEdit3;
    private javax.swing.JMenu jMenuEdit4;
    private javax.swing.JMenuItem jMenuExit;
    private javax.swing.JMenuItem jMenuExit1;
    private javax.swing.JMenuItem jMenuExit2;
    private javax.swing.JMenuItem jMenuExit3;
    private javax.swing.JMenuItem jMenuExit4;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenu jMenuFile1;
    private javax.swing.JMenu jMenuFile2;
    private javax.swing.JMenu jMenuFile3;
    private javax.swing.JMenu jMenuFile4;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuOpen;
    private javax.swing.JMenuItem jMenuOpen1;
    private javax.swing.JMenuItem jMenuOpen2;
    private javax.swing.JMenuItem jMenuOpen3;
    private javax.swing.JMenuItem jMenuOpen4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel labelProgress;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuBar menuBar1;
    private javax.swing.JMenuBar menuBar2;
    private javax.swing.JMenuBar menuBar3;
    private javax.swing.JMenuBar menuBar4;
    private javax.swing.JPanel panelImg;
    private javax.swing.JProgressBar progress;
    // End of variables declaration//GEN-END:variables

}

